/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.passport.service.plugins  
 */
package com.apache.passport.service.plugins;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.entity.LoginInfo;
import com.apache.passport.entity.Token;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.UctUserManager;

/**
 * description:  外部系统登录(socket形式,webservice)
 * 				 此种方式只返回token,下次手机端放的时候,都需要写到token进行传输
 * 				 例如,手机客户端,c#界面登录,以及其他语言的登录,全部使用这种形式或webservice
 * @author Hou Dayu 创建时间：2014-12-25  
 * @Copyright(c)2014:
 */
public class UserOwSocketLoginPlugin implements PluginConnector {
	protected IDao uctUserDao;
	private UctUserManager uctUserManager;
	private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	public void setUctUserDao(IDao uctUserDao) {
		this.uctUserDao = uctUserDao;
	}

	public void setUctUserManager(UctUserManager uctUserManager) {
		this.uctUserManager = uctUserManager;
	}

	/**
	 * TODO 例如手机客户端,C# 等语言的登录
	 * 下次手机端放的时候,都需要写到token进行传输
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		String addressIP = String.valueOf(vo.getParams("addressIP"));
		ResultEntity result = new ResultEntity();
		result.setResult("true");

		String userEname = (String) vo.getParams("userEname");
		if (Validator.isEmpty(userEname)) {
			result.setEntity("F");
			result.setMessage("用户名不能为空");
			return result;
		}
		String upass = (String) vo.getParams("userPass");
		if (Validator.isEmpty(upass)) {
			result.setEntity("F");
			result.setMessage("密码不能为空");
			return result;
		}
		UctUser obj = (UctUser) getInfoByEname(userEname);
		if (!Validator.isEmpty(obj)) {
			//判断密码是否正确
			if ((obj.getUserPass()).equalsIgnoreCase(upass)) {
				String randNum = PassportHelper.getInstance().createRandomString(12);
				Token token = new Token();
				String tokenId = Validator.generate() + randNum;
				token.setTokenId(tokenId);
				token.setUserEname(DesUtils.getInstance().encrypt(userEname));
				token.setUserCname(obj.getUserCname());
				token.setLoginTime(df.format(new Date()));
				token.setEmail(DesUtils.getInstance().encrypt(obj.getEmail()));
				token.setMobile(DesUtils.getInstance().encrypt(obj.getMobile()));
				token.setUserId(obj.getUserId());
				result.setEntity(token);
				result.setMessage("登录成功");
				LoginInfo loginInfo = new LoginInfo();
				loginInfo.setLgnLastipaddress(addressIP);
				loginInfo.setUserEname(userEname);
				token.setLoginInfo(loginInfo);
				//存储缓存,且每次传输必须携带tokenId,作为访问系统的唯一标识 
				SystemTools.getInstance().getCache("loginToken").createCacheObject(tokenId, token);
				return result;
			} else {
				result.setEntity("F");
				result.setMessage("密码错误");
				return result;
			}
		} else {
			result.setEntity("F");
			result.setMessage("用户不存在");
			return result;
		}
	}

	/**
	 * description:  根据ename查询用户信息
	 * @param vo
	 * @return  
	 * @author Hou Dayu 
	 * @update 2015年2月13日11:08:40 yxcui 增加手机号邮箱登陆
	 * 
	 */
	public Object getInfoByEname(String userEname) {
		if (Validator.isNull(userEname)) {
			return null;
		}
		String sql = uctUserDao.getSql(3);// from UctUser where userEname :=userEname

		String mobileLogin = SystemTools.getInstance().getValue("mobileLogin");
		String emailLogin = SystemTools.getInstance().getValue("emailLogin");

		if ("1".equals(mobileLogin)) {
			sql += "or mobile :=userEname ";
		}
		if ("1".equals(emailLogin)) {
			sql += "or email :=userEname ";
		}

		MethodParam param = new MethodParam("ByUserEname", "", sql, "com.apache.passport.entity.UctUser");
		param.setParams("userEname", userEname);
		if ("1".equals(mobileLogin)) {
			param.setParams("mobile", userEname);
		}
		if ("1".equals(emailLogin)) {
			param.setParams("email", userEname);
		}
		return uctUserDao.selectSingle(param);
	}

}
